package Gobang;

import java.util.Scanner;

/**
 * @author 张子健
 * @version 1.0
 * Time: 2022-09-25-9:57
 */
@SuppressWarnings({"all"})
public class Gugart {
    Chess chess = new Chess();  //棋子
    ChessBroad Board = new ChessBroad(); //棋盘
    Scanner myScanner = new Scanner(System.in);

    public static void main(String[] args) {
        Gugart gugart = new Gugart();
        gugart.BeginChess();    //开始落子
    }

    //开始游戏
    public void BeginChess() {
        Board.PrintBoard();    //棋盘遍历
        int x; //行索引
        int y; //列索引
        do {
            if (chess.JudgeColor() == chess.CHESSWHITE) {
                System.out.println("--------------------白棋行--------------------");
            } else {
                System.out.println("--------------------黑棋行--------------------");
            }

            System.out.print("请输入行坐标:");
            x = myScanner.nextInt();
            System.out.print("请输入列坐标:");
            y = myScanner.nextInt();

            if (x > Board.N || x <= 0 || y > Board.N || y <= 0) {
                System.out.println("索引位置错误,请重新输入坐标");
                continue;
            }

            if (Board.chessBoard[x - 1][y - 1] != '+') {
                System.out.println("该位置已经落子,请重新输入坐标");
                continue;
            }

            if (chess.JudgeColor() == chess.CHESSWHITE) {
                Board.chessBoard[x - 1][y - 1] = chess.CHESSWHITE;
            } else {
                Board.chessBoard[x - 1][y - 1] = chess.CHESSBLACK;
            }

            //判断输赢
            if (JudgeWin(x, y)) {
                Board.PrintBoard();
                if (chess.JudgeColor() == chess.CHESSWHITE) {
                    System.out.println("白棋获胜");
                } else {
                    System.out.println("黑棋获胜");
                }
                System.exit(0);
            }

            Chess.count++;  //总棋子数
            Board.PrintBoard();   //打印面板
        } while (Chess.count < Math.pow(Board.N, 2));
    }

    public boolean JudgeWin(int x, int y) {
        if (RightJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (UpJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (UpJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (LeftJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (DownJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (RightUpJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (RightDownJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (LeftDownJudge(x - 1, y - 1) >= 5) {
            return true;
        } else if (LeftUpJudge(x - 1, y - 1) >= 5) {
            return true;
        }
        return false;
    }

    //向上五连珠
    public int UpJudge(int x, int y) {
        int count = 1;

        while (x > 0 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x - 1][y]) {
                count++;
                x--;
            } else {
                return -1;
            }
        }

        return count;
    }

    //向右五连珠
    public int RightJudge(int x, int y) {
        int count = 1;

        while (y < Board.N - 1 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x][y + 1]) {
                count++;
                y++;
            } else {
                return -1;
            }
        }
        return count;
    }

    //向左五连珠
    public int LeftJudge(int x, int y) {
        int count = 1;

        while (y > 0 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x][y - 1]) {
                count++;
                y--;
            } else {
                return -1;
            }
        }
        return count;
    }

    //向下五连珠
    public int DownJudge(int x, int y) {
        int count = 1;

        while (x < Board.N - 1 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x + 1][y]) {
                count++;
                x++;
            } else {
                return -1;
            }
        }
        return count;
    }

    //向右上五连珠
    public int RightUpJudge(int x, int y) {
        int count = 1;

        while (x > 0 && y < Board.N - 1 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x - 1][y + 1]) {
                count++;
                x--;
                y++;
            } else {
                return -1;
            }
        }
        return count;
    }

    //向右下五连珠
    public int RightDownJudge(int x, int y) {
        int count = 1;

        while (x < Board.N - 1 && y < Board.N - 1 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x + 1][y + 1]) {
                count++;
                x++;
                y++;
            } else {
                return -1;
            }
        }
        return count;
    }

    //向左下五连珠
    public int LeftDownJudge(int x, int y) {
        int count = 1;

        while (x < Board.N - 1 && y > 0 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x + 1][y - 1]) {
                count++;
                x++;
                y--;
            } else {
                return -1;
            }
        }
        return count;
    }

    //向左上五连珠
    public int LeftUpJudge(int x, int y) {
        int count = 1;

        while (x > 0 && y > 0 && count < 5) {
            if (Board.chessBoard[x][y] == Board.chessBoard[x - 1][y - 1]) {
                count++;
                x--;
                y--;
            } else {
                return -1;
            }
        }
        return count;
    }
}
